<?php


namespace app\api\wxpay;

class PayMktTransfers
{
	public static function transfers($conf, $order_sn, $amount, $openid, $desc, $check_name = false, $user_name = "")
	{
		define("WxPay_AppId", $conf["AppId"]);
		define("WxPay_MerchantId", $conf["MerchantId"]);
		define("WxPay_ApiKey", $conf["ApiKey"]);
		define("WxPay_AppSecret", $conf["AppSecret"]);
		$apiclient_cert = get_cert_path("system", "wechat.apiclient_cert.pem", $conf["apiclient_cert"]);
		$apiclient_key = get_cert_path("system", "wechat.apiclient_key.pem", $conf["apiclient_key"]);
		$total_amount = 100 * $amount;
		$data = ["mch_appid" => $conf["AppId"], "mchid" => $conf["MerchantId"], "nonce_str" => self::createNoncestr(), "partner_trade_no" => $order_sn, "openid" => $openid, "check_name" => $check_name ? "FORCE_CHECK" : "NO_CHECK", "re_user_name" => $user_name, "amount" => $total_amount, "desc" => $desc, "spbill_create_ip" => ""];
		$secrect_key = $conf["ApiKey"];
		$data = array_filter($data);
		ksort($data);
		$str = "";
		foreach ($data as $k => $v) {
			$str .= $k . "=" . $v . "&";
		}
		$str .= "key=" . $secrect_key;
		$data["sign"] = md5($str);
		$xml = self::arraytoxml($data);
		$url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
		$res = self::wx_curl($apiclient_cert, $apiclient_key, $xml, $url);
		$return = self::xmltoarray($res);
		if ($return["return_code"] == "SUCCESS") {
			if ($return["result_code"] == "SUCCESS") {
				return true;
			} else {
				return $return;
			}
		} else {
			return $return;
		}
		$responseObj = simplexml_load_string($res, "SimpleXMLElement", LIBXML_NOCDATA);
	}
	public static function createNoncestr($length = 32)
	{
		$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
		$str = "";
		for ($i = 0; $i < $length; $i++) {
			$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
		}
		return $str;
	}
	public static function arraytoxml($data)
	{
		$str = "<xml>";
		foreach ($data as $k => $v) {
			$str .= "<" . $k . ">" . $v . "</" . $k . ">";
		}
		$str .= "</xml>";
		return $str;
	}
	public static function xmltoarray($xml)
	{
		libxml_disable_entity_loader(true);
		$xmlstring = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
		$val = json_decode(json_encode($xmlstring), true);
		return $val;
	}
	public static function wx_curl($apiclient_cert, $apiclient_key, $vars, $url, $second = 30, $aHeader = [])
	{
		$sslcert_path = $apiclient_cert;
		$sslkey_path = $apiclient_key;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, $second);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_SSLCERTTYPE, "PEM");
		curl_setopt($ch, CURLOPT_SSLCERT, $sslcert_path);
		curl_setopt($ch, CURLOPT_SSLKEYTYPE, "PEM");
		curl_setopt($ch, CURLOPT_SSLKEY, $sslkey_path);
		if (count($aHeader) >= 1) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
		}
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
		$data = curl_exec($ch);
		if ($data) {
			curl_close($ch);
			return $data;
		} else {
			$error = curl_errno($ch);
			echo "call faild, errorCode:{$error}\n";
			curl_close($ch);
			return false;
		}
	}
}